package cn.labbook.bluetooth.controller.v2;

import cn.labbook.bluetooth.common.MyPage;
import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.controller.AbstractController;
import cn.labbook.bluetooth.entity.Ammeter;
import cn.labbook.bluetooth.entity.ApplyRecord;
import cn.labbook.bluetooth.entity.User;
import cn.labbook.bluetooth.pojo.dto.ApplyRecordDTO;
import cn.labbook.bluetooth.service.IAmmeterService;
import cn.labbook.bluetooth.service.IApplyRecordService;
import cn.labbook.bluetooth.service.IConstantService;
import cn.labbook.bluetooth.service.IUserService;
import cn.labbook.bluetooth.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: zhangyang
 * @Date: 2023/4/13 11:16
 * @Desc: v2版本 微信接口
 */
@Controller
@RequestMapping("/v2/wx")
@Slf4j
public class V2WXController extends AbstractController {

    @Autowired
    private IAmmeterService ammeterService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IApplyRecordService applyRecordService;
    @Autowired
    private IConstantService constantService;


    @RequestMapping("/getAmmeter")
    @ResponseBody
    public ResultDto getAmmeter(String ammeterId) {
        return success(ammeterService.getAmmeter(ammeterId));
    }

    @RequestMapping("/getProtectPrice")
    @ResponseBody
    public ResultDto getProtectPrice() {
        return success(constantService.getProtectPrice());
    }

    @RequestMapping("/guestApply")
    @ResponseBody
    public ResultDto guestApply(String choiceDate, String userId, MyPage<ApplyRecord> page) {
        User user = userService.getById(userId);
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(choiceDate)) {
            map.put("time", choiceDate);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            map.put("time", sdf.format(new Date()));
        }

        // 到账金额
        Double totalAmount = 0.0d;
        // 申请金额
        Double applyAmount = 0.0d;
        List<ApplyRecord> sumPay = applyRecordService.getSumPay(choiceDate, user.getAdminId(), null);
        for (ApplyRecord applyRecord : sumPay) {
            if (Objects.isNull(applyRecord)) {
                continue;
            }

            if (Objects.equals(ApplyRecord.Status.IN.getKey(), applyRecord.getStatus())) {
                applyAmount += applyRecord.getAmount();
            } else if (Objects.equals(ApplyRecord.Status.COMPLETE.getKey(), applyRecord.getStatus())) {
                totalAmount += applyRecord.getAmount();
            }
        }
        BigDecimal bigDecimal = new BigDecimal(totalAmount);
        Double s = Double.parseDouble(bigDecimal.setScale(2, RoundingMode.HALF_UP).toString());
        BigDecimal bigDecimal1 = new BigDecimal(applyAmount);
        Double s1 = Double.parseDouble(bigDecimal1.setScale(2, RoundingMode.HALF_UP).toString());
        map.put("totalAmount", s);
        map.put("applyAmount", s1);

        List<ApplyRecordDTO> list = applyRecordService.listWithLandlord(choiceDate, user.getAdminId(), page);
        map.put("list", list);
        return success(map);
    }

    //房东小程序批量修改保电金额
    @RequestMapping("/editProtectPrice")
    @ResponseBody
    public ResultDto editProtectPrice(String ammeterIds, double protectPrice){
        String[] split = ammeterIds.split(",");
        List<Ammeter> list = new ArrayList<>();
        for(String ammeterId : split) {
            Ammeter ammeter = ammeterService.getById(ammeterId);
            if (Objects.isNull(ammeter)) {
                continue;
            }

            ammeter.setProtectPrice(protectPrice);
            list.add(ammeter);
        }

        ammeterService.updateBatchById(list);
        return success();
    }

    @RequestMapping("/updateQrCode")
    @ResponseBody
    public ResultDto updateQrCode(String userId, String qrCodePath, Integer type){
        userService.updateQrCode(userId, qrCodePath, type);
        return success();
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public ResultDto getUser(String userId){
        return success(userService.getUser(userId));
    }

    @RequestMapping("/readNotice")
    @ResponseBody
    public ResultDto readNotice(String userId){
        userService.readNotice(userId);
        return success();
    }

    @RequestMapping("/getQrCode")
    @ResponseBody
    public ResultDto getQrCode(String ammeterId){
        return success(ammeterService.getQrCode(ammeterId));
    }

}
